const {
  Bed,
  validateBed
} = require('../../../model/Bed');

module.exports = async (req, res) => {
  const beds = req.fields;
  const result = [];
  for (let key in beds) {
    // 数据格式校验
    const {
      error
    } = validateBed(beds[key]);
    // 格式不符合要求
    if (error) return res.sendResult(null, 400, error.message);
  }

  for (let key in beds) {
    const bed = beds[key];

    if (bed._id) {
      result.push(await Bed.findOneAndUpdate({
        _id: bed._id
      }, bed));
    } else {
      result.push(await Bed.create(bed));
    }
  }

  return res.sendResult(result, 200, '更新床位成功');
};